home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / grp / grpread.c < prev    next >
C/C++ Source or Header  |  1992-03-19  |  3KB  |  136 lines

  1. /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #include <errno.h>
  21. #include <stddef.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <sys/types.h>
  26. #include <grp.h>
  27.  
  28. /* This is the function that all the others are based on.
  29.    The format of the group file is known only here.  */
  30.  
  31. /* Structure containing info kept by each __grpread caller.  */
  32. typedef struct
  33.   {
  34.     char *buf;
  35.     size_t buflen;
  36.     size_t max_members;
  37.     char **members;
  38.     struct group g;
  39.   } grpread_info;
  40.  
  41.  
  42. /* Return a chunk of memory containing a pre-initialized `grpread_info'.  */
  43. PTR
  44. DEFUN_VOID(__grpalloc)
  45. {
  46.   grpread_info *info = (PTR) malloc (sizeof(grpread_info));
  47.   if (info == NULL)
  48.     return NULL;
  49.  
  50.   info->buf = NULL;
  51.   info->buflen = 0;
  52.  
  53.   info->max_members = 5;
  54.   info->members = (char **) malloc (5 * sizeof(char *));
  55.   if (info->members == NULL)
  56.     {
  57.       free ((PTR) info);
  58.       return NULL;
  59.     }
  60.  
  61.   return info;
  62. }
  63.  
  64. /* Read a group entry from STREAM, filling in G.  */
  65. struct group *
  66. DEFUN(__grpread, (stream, g), FILE *stream AND PTR CONST g)
  67. {
  68.   register grpread_info *CONST info = (grpread_info *) g;
  69.   char *start, *end;
  70.   register size_t i;
  71.  
  72.   /* Idiocy checks.  */
  73.   if (stream == NULL)
  74.     {
  75.       errno = EINVAL;
  76.       return NULL;
  77.     }
  78.  
  79.   do
  80.     if (__getline (&info->buf, &info->buflen, stream) == -1)
  81.       return NULL;
  82.   while (info->buf[0] == '#');
  83.  
  84.   start = info->buf;
  85.   end = strchr (start, ':');
  86.   if (end == NULL)
  87.     return NULL;
  88.   *end = '\0';
  89.   info->g.gr_name = start;
  90.  
  91.   start = end + 1;
  92.   end = strchr (start, ':');
  93.   if (end == NULL)
  94.     return NULL;
  95.   *end = '\0';
  96.   info->g.gr_passwd = start;
  97.  
  98.   info->g.gr_gid = (gid_t) strtol (end + 1, &end, 10);
  99.   if (*end != ':')
  100.     return NULL;
  101.  
  102.   i = 0;
  103.   do
  104.     {
  105.       start = end + 1;
  106.       end = strchr (start, ',');
  107.       if (end == NULL)
  108.     {
  109.       end = strchr (start, '\n');
  110.       if (end == start)
  111.         break;
  112.       if (end == NULL)
  113.         return NULL;
  114.       *end = '\0';
  115.       end = NULL;
  116.     }
  117.       else
  118.     *end = '\0';
  119.  
  120.       if (i == info->max_members - 2)
  121.     {
  122.       info->max_members += 5;
  123.       info->members = (char **)
  124.         realloc ((PTR) info->members, info->max_members * sizeof (char *));
  125.       if (info->members == NULL)
  126.         return NULL;
  127.     }
  128.  
  129.       info->members[i++] = start;
  130.     } while (end != NULL);
  131.   info->members[i] = NULL;
  132.   info->g.gr_mem = info->members;
  133.  
  134.   return &info->g;
  135. }
  136.